package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.igas.momp.facade.bean.McListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryMcListInfoRequest;
import cc.rengu.redp.bizimpl.upmp.bean.MarketManagerRequest;
import cc.rengu.redp.bizimpl.upmp.service.CommissionMarketService;
import cc.rengu.redp.bizimpl.upmp.service.ICustomerMarketService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "营销管理:营销活动审核")
@Slf4j
@RestController
@RequestMapping("/admin/market/check")
public class MarketCheckController extends BaseController {
    @Autowired
    private ICustomerMarketService customerMarketService;
    @Autowired
    private CommissionMarketService commissionMarketService;

    //查询营销活动
//    @RequiresPermissions("营销管理:营销活动审核")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public IPage<McListInfoBean> findByPage(QueryRequest queryRequest, QueryMcListInfoRequest reqDto, HttpServletRequest request) throws RedpException {
        reqDto.setQueryType("0");
        reqDto.setQueryRole("01");
        return customerMarketService.findMcByPage(queryRequest, reqDto);
    }

    //审核
//    @RequiresPermissions("营销活动审核:审核")
    @RequestMapping(value = "/checkSubmit", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult checkSave(MarketManagerRequest entity) throws RedpException {
        if (StringUtils.isBlank(entity.getMcNo())) {
            throw new RedpException("营销活动编号不能为空");
        }
        if ("1".equals(entity.getOprType())) {
            entity.setMsg("营销活动审核通过");
        } else if ("2".equals(entity.getOprType())) {
            entity.setMsg("营销活动审核拒绝");
        } else {
            throw new RedpException("营销活动审核时操作类型无效，请联系管理员");
        }
        entity.setQueryType("0");
        entity.setQueryRole("01");
        //根据营销活动类型调用不同审核接口
        if ("00".equals(entity.getMcType()) || "01".equals(entity.getMcType()) || "02".equals(entity.getMcType()) || "03".equals(entity.getMcType())) {
            customerMarketService.manageMoneyOffMc(entity);
        } else if ("10".equals(entity.getMcType()) || "11".equals(entity.getMcType())) {
            customerMarketService.manageCouponMc(entity);
        } else if ("20".equals(entity.getMcType())) {
            commissionMarketService.manageMchntFeeDiscountMc(entity);
        } else {
            log.error("请求参数有误,营销活动类型的值无效,mcType=[{}]", entity.getMcType());
            throw new RedpException("请求参数有误,营销活动类型的值无效，请联系管理员");
        }
        return OperationResult.buildSuccessResult(entity.getMsg() + "成功");
    }
}
